import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView/HomeView.vue'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '',
    redirect: '/home'
  },
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [
      {
        path: 'follow',
        name: 'FollowView',
        component: () => import('@/views/HomeView/FollowContentView.vue')
      },
      {
        path: 'recomment',
        alias: '',
        name: 'RecommendView',
        component: () => import('@/views/HomeView/RecommendContentView.vue')
      },
      {
        path: 'video',
        name: 'VideoView',
        component: () => import('@/views/HomeView/VideoContentView.vue')
      },
      {
        path: 'article',
        name: 'ArticleView',
        component: () => import('@/views/HomeView/ArticleContentView.vue')
      },
      {
        path: 'photo',
        name: 'PhotoView',
        component: () => import('@/views/HomeView/PhotoContentView.vue')
      },
      {
        path: 'Essence',
        name: 'EssenceView',
        component: () => import('@/views/HomeView/EssenceContentView.vue')
      },
      {
        path: 'BombSquad',
        name: 'BombSquadView',
        component: () => import('@/views/HomeView/BombSquadContentView.vue')
      },
    ],
  },
  {
    path: '/dynamic',
    name: 'dynamic',
    component: () => import('../views/DynamicView/DynamicView.vue'),
    children: [
      {
        path: 'Nearby',
        name: 'NearbyContentView',
        alias: '',
        component: () => import('@/views/DynamicView/NearbyContentView.vue')
      },
      {
        path: 'DynamicFollow',
        name: 'DynamicFollowContentView',
        component: () => import('@/views/DynamicView/DynamicFollowContentView.vue')
      },
      {
        path: 'Topics',
        name: 'TopicsContentView',
        component: () => import('@/views/DynamicView/TopicsContentView.vue')
      },
    ],
  },
  {
    path: '/live',
    name: 'live',
    component: () => import('../views/LiveView/LiveView.vue')
  },
  {
    path: '/little-page',
    name: 'little-page',
    component: () => import('../views/LittlePageView/LittlePageView.vue'),
    beforeEnter(to, from, next) {
      let token = window.localStorage.getItem('token')
      if (!token) {
        next('/beforeLogin')
        return
      } else {
        next()
      }
    }
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/UserView/UserView.vue')
  },
  {
    path: '/beforeLogin',
    name: 'beforeLogin',
    component: () => import('@/views/UserView/BeforeLoginView.vue'),
  },
  {
    path: '/allBomb',
    name: 'allBomb',
    component: () => import('@/views/HomeView/AllBombView.vue')
  },
  {
    path: '/comment',
    name: 'comment',
    component: () => import('@/views/HomeView/CommentView.vue')
  },
  {
    path: '/VideoInside/:index',
    name: 'VideoInside',
    component: () => import('@/views/HomeView/VideoInsideView.vue')
  },
  {
    path: '/search/:keywords',
    name: 'SearchView',
    component: () => import('@/views/HomeView/SearchView.vue')
  },
  {
    path: '/user-inside/:id',
    name: 'userInside',
    component: () => import('@/views/HomeView/UserInsideView.vue')
  },
  {
    path: '/user-followed',
    name: 'userFollowed',
    component: () => import('@/views/UserView/FollowedView.vue')
  },
  {
    path: '/user-history',
    name: 'historyView',
    component: () => import('@/views/UserView/HistoryView.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
